﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Xml.Linq;
using System.Runtime.Serialization.Json;

public partial class login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string dataFromClient = Server.UrlDecode(Request.Form.ToString());
        if (Request.QueryString["act"] == null || Request.QueryString["username"] == null || Request.QueryString["password"] == null)
        {
            Response.Write("validate=false");
        }
        else
        {
            informationToBeenSend myInformationToBeenSend;
            switch (Request.QueryString["act"])
            {
                case "login":
                    myInformationToBeenSend = new informationToBeenSend(Request.QueryString["username"], Request.QueryString["password"]);
                        string domain = URL.getDomain(Request.Url.ToString());
                        string domainSuf = URL.getDomainSuf(domain);
                        string currentDir = MapPath("") + @"\userdata\" + Request.QueryString["username"] + @"\";
                        user userInstance = new user(currentDir + @"user.xml");
                        userInstance.Cc = myInformationToBeenSend.checkCode == null ? "" : myInformationToBeenSend.checkCode;//save the check code in the xml file.
                        userInstance.Lt = myInformationToBeenSend.lastTime == null ? "" : myInformationToBeenSend.lastTime;
                        userInstance.setAllValue();
                        userInstance.saveToFile(currentDir + @"user.xml");
                        Response.Write(myInformationToBeenSend.information);
                        break;
                default:
                        break;
            }
        }
    }

    private bool UserConfirmation(string username, string password)  //I haven't test this function!
    {
        string newDir = MapPath("") + @"\userdata\" + username + @"\";
        if (Directory.Exists(newDir))
        {
            XElement Xe = XElement.Load(newDir+"user.xml");
            string tmpPassword = Xe.Element("ps").Value.ToString();
            if (password == tmpPassword)
                return true;
        }
        return false;
    }

}
